#include "MenuButton.h"
#include "stdio.h"

MenuButton::MenuButton(void)
{
	mCenter = D3DXVECTOR3(0.0f,0.0f,0.0f);
	mFont = NULL;
	mSprite = NULL;
	mTextureLit = NULL;
	mTextureUnlit = NULL;
	mIsPressed = false;
	mText = L" ";

	m_lit = false;
	screenX = 0;
	screenY = 0;

	xf = 0;
	yf = 0;

	idNum = 0;

}

MenuButton::~MenuButton(void)
{
	// release the textures
	//if(!mTextureLit)
	mTextureLit->Release();
	//if(!mTextureUnlit)
	//mTextureUnlit->Release();
}

MenuButton::MenuButton(UINT id, float xFrac, float yFrac, float bbWidth, float bbHeight, int h, int w, LPCWSTR texLit, LPCWSTR text, ID3DXSprite* sprite, ID3DXFont* font, HWND wnd)
{
	mPlaySound = true;

	idNum = id;

	xf = xFrac;
	yf = yFrac;

	screenX = xf * bbWidth;
	screenY = yf * bbHeight;

	mCenter = D3DXVECTOR3((float)w/2, (float)h/2, 0.0f);
	mFont = font;
	mSprite = sprite;
	mText = text;
	//mText = new char[256];
	//strcpy((char*)mText, text);

	// create the textures
	D3DXCreateTextureFromFileEx(m_pD3DDevice, texLit, w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 0, NULL, &mTextureLit);
	//D3DXCreateTextureFromFileEx(m_pD3DDevice, texUnlit, w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 0, NULL, &mTextureUnlit);

	mWnd = wnd;

	mIsPressed = false;
}

MenuButton::MenuButton(UINT id, float xFrac, float yFrac,float bbWidth, float bbHeight,int h, int w, LPCWSTR tex, ID3DXSprite* sprite, HWND wnd)
{
	mPlaySound = true;

	idNum = id;

	xf = xFrac;
	yf = yFrac;

	screenX = xf * bbWidth;
	screenY = yf * bbHeight;

	mCenter = D3DXVECTOR3((float)w/2, (float)h/2, 0.0f);
	mSprite = sprite;

	// create the textures
	D3DXCreateTextureFromFileEx(m_pD3DDevice, tex, w, h, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 0, NULL, &mTextureLit);

	mWnd = wnd;

	mIsPressed = false;
}

bool MenuButton::isMouseOver()
{
	POINT p;

	// simple function to get cursor coordinates
	// DirectInput is a PITA...
	GetCursorPos(&p);

	ScreenToClient(mWnd, &p);

	float x1 = screenX - mCenter.x;
	float y1 = screenY - mCenter.y;
	float x2 = screenX + mCenter.x;
	float y2 = screenY + mCenter.y;

	if (p.x > x1 &&
		p.x < x2 &&
		p.y > y1 &&
		p.y < y2 )
	{
		// if the cursor is over it, play a sound effect
		if (mPlaySound)
		{
			//			SOUNDMANAGER->SoundPlay(SOUND_MENUCLICK2);
			mPlaySound = false;
		}

		return true;
	}
	else
	{
		// if they take the cursor away, allow the sound to be triggered again
		mPlaySound = true;
		return false;
	}	
}

void MenuButton::Render(bool map)
{
	// renders the button based on the current state and displays text (format
	// given by the passed text object.
	// assumes the D3D rendering process has already begun
	D3DXVECTOR3 pos;
	pos.x = screenX;
	pos.y = screenY;
	pos.z = 0.0f;
	D3DXCOLOR textcolor;
	textcolor = D3DCOLOR_ARGB(255,0,0,0);


	if(map)
	{
		if(mIsPressed)
		{
			mSprite->Draw(mTextureLit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 0, 0));
		}
		else if(!mIsPressed)

		{
			mSprite->Draw(mTextureLit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));
		}
	}
	else
	{
		//if (m_lit)
		//{	textcolor = D3DCOLOR_ARGB(255,255,0,0);	}
		//else
		//{	textcolor = D3DCOLOR_ARGB(255,0,0,0);	}

		//if(mIsPressed)
		//{
		//	mSprite->Draw(mTextureLit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));
		//}
		//else
		//{
		//	mSprite->Draw(mTextureUnlit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));
		//}

		mSprite->Draw(mTextureLit, 0, &mCenter, &pos, D3DCOLOR_ARGB(255, 255, 255, 255));

		RECT R;

		R.bottom	= screenY + mCenter.y;
		R.left		= screenX - mCenter.x;
		R.right		= screenX + mCenter.x;
		R.top		= screenY - mCenter.y;

		textcolor = D3DCOLOR_ARGB(255,0,255,0);
		mFont->DrawText(mSprite, mText, -1, &R, DT_NOCLIP | DT_CENTER | DT_VCENTER, textcolor);
	}
}

void MenuButton::UpdateSize(float bbWidth, float bbHeight)
{
	screenX = xf * bbWidth;
	screenY = yf * bbHeight;
}